package com.lovo.atm;

import javax.swing.*;

/**
 * ATM类（对象版本）
 */
public class ATM {
    private User[] sysUser = new User[]{
            new User("123456","111111",1000),
            new User("654321","222222",2000),
            new User("666666","333333",3000),
            new User("888888","444444",4000),
            new User("999999","555555",5000),
    };
    private int index;

    public void process(){
            for (;;) {
                // 登录成功的下标值
                index = 0;
                boolean isLogin = false;
                for (int i = 0; i < 3; i++) {
                    isLogin = login();
                    if (isLogin) {
                        break;
                    } else {
                        if (i < 2) {
                            JOptionPane.showMessageDialog(null,"账号密码错误，请重新输入，还剩" + (2 - i) + "次");
                        } else {
                            JOptionPane.showMessageDialog(null,"连续三次错误，卡被吞掉");
                            System.exit(0);
                        }
                    }
                }
                if(isLogin) {
                    menu();
                }

            }


    }
    public boolean login(){
        String account = JOptionPane.showInputDialog(null,"请输入账号：");
        String pwd = JOptionPane.showInputDialog(null,"请输入密码：");
        for (int i = 0; i < sysUser.length; i++) {
            if (sysUser[i].validate(account,pwd)) {
                index = i;
                return true;
            }
        }
        return false;
    }
    public void menu(){
        while(true) {
            String choose = JOptionPane.showInputDialog(null,"请选择：\n" +
                    "1-查看余额\n" +
                    "2-存款\n" +
                    "3-取款\n" +
                    "4-改密\n" +
                    "5-登出\n" +
                    "6-退出\n" +
                    "7-转账 ");
            if ("1".equals(choose)) {
                search();
            } else if ("2".equals(choose)) {
                save();
            } else if ("3".equals(choose)) {
                get();
            } else if ("4".equals(choose)) {
                changePwd();
            } else if ("5".equals(choose)) {
                System.out.println("账号已退出");
                break;
            } else if ("6".equals(choose)) {
                System.out.println("Bye");
                System.exit(0);
            } else if ("7".equals(choose)) {
                transfer();
            } else {
                System.out.println("输入有误");
            }
        }
    }
    public void save(){
        String blance = JOptionPane.showInputDialog(null,"请输入存款的金额：");
        sysUser[index].add(Double.parseDouble(blance));
        JOptionPane.showMessageDialog(null,"存款成功");

    }
    public void get(){
        String blance = JOptionPane.showInputDialog(null,"请输入取款的金额：");
        if(sysUser[index].minus(Double.parseDouble(blance))){
            JOptionPane.showMessageDialog(null,"取款成功");
        }else{
            JOptionPane.showMessageDialog(null,"余额不足");
        }
    }
    public void search(){
        JOptionPane.showMessageDialog(null,"您的余额为："+sysUser[index].getBlance());
    }
    public void changePwd(){

    }
    public void transfer(){
        // 输入转账的账号
        String otherAccount = JOptionPane.showInputDialog(null,"请输入对方账号：");
        // 判断账号是否存在
        boolean isExist = false;
        int otherIndex = 0;
        for(int i = 0;i < sysUser.length;i++) {
            if(sysUser[i].equals(otherAccount)) {
                isExist = true;
                otherIndex = i;
                break;
            }
        }
        if(!isExist) {
            JOptionPane.showMessageDialog(null,"账号不存在");
            return;
        }
        // 判断转账的金额是否超过当前账户的余额
        String blance = JOptionPane.showInputDialog(null,"请输入转账金额：");
        // 如果余额不足，给出提示，并回到菜单。如果余额足够，则将钱从当前账户扣去
       if(!sysUser[index].minus(Double.parseDouble(blance))){
           JOptionPane.showMessageDialog(null,"余额不足");
           return;
       }
        // 将转账的金额加到对方账号
        sysUser[otherIndex].add(Double.parseDouble(blance));
        JOptionPane.showMessageDialog(null,"转账成功");
    }
}
